
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:syria_sports/core/app_export.dart' ;
import 'package:syria_sports/core/utils/show_toast.dart';

import '../../../data/controllers/attendances_controller.dart';
import '../../../data/models/auth/user.dart';
import '../../../dialogs/delete_player_confirmation.dart';
import '../models/attendance.dart';

class UserAttendanceItemView extends StatelessWidget {
  final Attendance attendance;
  final User user;
   const UserAttendanceItemView({super.key, required this.attendance, required this.user});

  @override
  Widget build(BuildContext context) {


    return Slidable(
      enabled: PrefUtils.isGymAdmin(),
      // The start action pane is the one at the left or the top side.
      startActionPane: ActionPane(
        // A motion is a widget used to control how the pane animates.
        motion: const ScrollMotion(),

        // All actions are defined in the children parameter.
        children: [
          // A SlidableAction can have an icon and/or a label.
          SlidableAction(
            onPressed: (context){
              showDeleteConfirmation(context, attendance);
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.red,
            icon: Icons.delete,
            label: 'حذف',
          ),
        ],
      ),
      child: Card(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  const Icon(Icons.sports_gymnastics,color: Colors.deepOrange,),
                  const SizedBox(width: 4),
                  Text(attendance.exercise!.name,style: theme.textTheme.titleMedium!.copyWith(color: Colors.deepOrange)),
                ],
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  attendance.gym!.name,
                  overflow: TextOverflow.ellipsis,
                  style: theme.textTheme.displaySmall,
                  maxLines: 2,
                ),
              ),
              Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Icon(Icons.date_range,color: Colors.red,),
                  SizedBox(width: 4),
                  Text(attendance.datetime.format(), style: theme.textTheme.displaySmall),
                  SizedBox(width: 4),
                  Icon(Icons.access_time_outlined,color: Colors.red,),
                  SizedBox(width: 4),
                  Text(DateFormat('hh:mm').format(attendance.datetime), style: theme.textTheme.displaySmall),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
  void showDeleteConfirmation(BuildContext context, Attendance subscription) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: (subscription.user?.fullName)??user.fullName,
          titleUser: 'حضور اللاعب',
          onConfirm: () {
            AttendancesController.deleteById(subscription.attendanceId??'-1');
          },
        );
      },
    );
  }
}
